Skip to main content

Spring Hibernate

- ORM  - HQL  - lazy initialization vs Eager loading @OneToMany(fetch = FetchType.LAZY)

Session Factory/Session/Transaction

lazy initialization vs Eager loading

  • Required: Code will always run in a transaction. Creates a new transaction or reuses one if available.
  • Requires_new: Code will always run in a new transaction. Suspends the current transaction if one exists.
  • Supports
  • Mandatory
  • Nested

Isolation

Defines the data contract between transactions.

  • Read Uncommitted: Allows dirty reads.
  • Read Committed: Does not allow dirty reads.
  • Repeatable Read: If a row is read twice in the same transaction, the result will always be the same.
  • Serializable: Performs all transactions in a sequence.

The different levels have different performance characteristics

Example of when a dirty read can occur:

thread 1 thread 2

| |

write(x) |

| |

| read(x)

| |

rollback |

v v

value (x) is now dirty (incorrect)

get() loads the data as soon as it’s called whereas load() returns a proxy object and loads data only when it’s actually required, so load() is better because it support lazy loading

types of objcets

Transient- never persisted Persistent - persisted state Detached - after persisted out of scope

Merge call - update

Cascading:

  1. None: No Cascading, it’s not a type but when we don’t define any cascading then no operations in parent affects the child.
  2. ALL: Cascades save, delete, update, evict, lock, replicate, merge, persist. Basically everything
  3. SAVE_UPDATE: Cascades save and update, available only in hibernate.
  4. DELETE: Corresponds to the Hibernate native DELETE action, only in hibernate.
  5. DETATCH, MERGE, PERSIST, REFRESH and REMOVE – for similar operations
  6. LOCK: Corresponds to the Hibernate native LOCK action.
  7. REPLICATE: Corresponds to the Hibernate native REPLICATE action.

Scopes of Spring Beans:

here are five scopes defined for Spring Beans.

  1. singleton: Default, Only one instance of the bean will be created for each container. This is the default scope for the spring beans. While using this scope, make sure spring bean doesn’t have shared instance variables otherwise it might lead to data inconsistency issues because it’s not thread-safe.
  2. prototype: A new instance will be created every time the bean is requested.
  3. request: This is same as prototype scope, however it’s meant to be used for web applications. A new instance of the bean will be created for each HTTP request.
  4. session: A new bean will be created for each HTTP session by the container.
  5. global-session: This is used to create global session beans for Portlet applications

Autowiring ways:

  1. autowire byName
  2. autowire byType
  3. autowire by constructor
  4. autowiring by @Autowired and @Qualifier annotations

Annotations

@Component is used to indicate that a class is a component. These classes are used for auto-detection and configured as bean when annotation based configurations are used. @Controller is a specific type of component, used in MVC applications and mostly used with RequestMapping annotation. @RestController = @Controller + @ResponseBody @Repository annotation is used to indicate that a component is used as repository and a mechanism to store/retrieve/search data. We can apply this annotation with DAO pattern implementation classes. @Service is used to indicate that a class is a Service. Usually, the business facade classes that provide some services are annotated with this.

We can use any of the above annotations for a class for auto-detection but different types are provided so that you can easily distinguish the purpose of the annotated classes.

  1. Some of the Spring annotations that I have used in my project are:
  • @Controller – for controller classes in Spring MVC project.
  • @RequestMapping – for configuring URI mapping in controller handler methods. This is a very important annotation, so you should go through Spring MVC RequestMapping Annotation Examples
  • @ResponseBody – for sending Object as response, usually for sending XML or JSON data as response.
  • @PathVariable – for mapping dynamic values from the URI to handler method argumets.
  • @Autowired – for autowiring dependencies in spring beans.
  • @Qualifier – with @Autowired annotation to avoid confusion when multiple instances of bean type is present.
  • @Service – for service classes.
  • @Scope – for configuring scope of the spring bean.
  • @Configuration@ComponentScan and @Bean – for java based configurations.
  • AspectJ annotations for configuring aspects and advices, @Aspect@Before@After@Around@Pointcut etc.